var member_id = ws = pictures = obj = store_id = '';
var remuneration = 0;
var title,targettime,remuneration,place,address,contacts,contacts_tel,demand='';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	store_id = plus.storage.getItem('store_id');
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	$('.mobilepic').on('click',function(){
		obj = $('.mobilepic').attr('data-pic');
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
	$('.release').on('click',function(){
		title = $('.campus_title').val();
		target_time = $('.campus_datatime').val();
		remuneration=  $('.campus_price').val();
		place = $('.campus_place').val();
		address = $('.campus_address').val();
		contacts = $('.campus_lxr').val();
		contacts_tel = $('.campus_tel').val();
		demand = $('.campus_xq').val();
		var str = target_time.replace(/-/g,'/'); // 将-替换成/，因为下面这个构造函数只支持/分隔的日期字符串
		var date = new Date(str);
		var time = date.getTime();
		targettime = time / 1000;
		var timestamp = Date.parse(new Date()) / 1000;                //当前时间
//		console.log(timestamp+'--'+targettime);return;
		if(!title){
		 	toast('标题不能为空！');return;
		}
		if(title.length < 2 || title.length > 12){
		 	toast('标题在2到12字符之间！');return;
		}
		if(!target_time){
		 	toast('时间不能为空！');return;
		}
		if(timestamp > targettime){
			toast('服务时间必须大于当前时间');return;
		}
		if(remuneration <= 0){
			toast('金额必须大于0！');return;
		}
		if(!place){
		 	toast('地址不能为空！');return;
		}
		if(!address){
		 	toast('详细地址不能为空！');return;
		}
		if(!contacts_tel){
		 	toast('电话不能为空！');return;
		}
		if(!contacts_tel.match(p1)){
		 	toast('请输入正确的手机号码！');return;
		}
		if(!demand){
		 	toast('需求不能为空！');return;
		}
		console.log(remuneration);
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"余额支付"},{title:"支付宝支付"},{title:"微信支付"}]},function(e){
			if(e.index == 1){
				$('.password-of-pay-wraper').css("display","block");
				$('#password_backdrop').css("display","block");
				$('.btn').slideUp()				
				
				//如果用户选择的是余额支付，则执行如下代码
				tool.shade(function (){
					$("#j_password_of_pay").hide();
				}).show();  
				//默认让第一个输入框获得焦点
				setTimeout(function (){
					$("#j_password_of_pay").show().find("input:password").eq(0).focus();
				},10); 
				var pwdInputs = $("#j_password_of_pay input:password");
				pwdInputs.each(function (index,item){
					$(this).on("keyup",function (){
						if(!tool.isEmpty($(this).val())){
							$(this).next().focus();
							//如果是最后一个输入框，则执行ajax操作
							if(index == pwdInputs.size() - 1){
								var pwd = "";
								$(this).blur();
								pwdInputs.each(function (){
									pwd += $(this).val();
								});
								yuezhifu(pwd); 
							}
						}
					});
				});
				
			}else if(e.index == 2){
				ptype = 'alipay';
				payment(remuneration);
			}else if(e.index == 3){
				ptype = 'wxpay';
				payment(remuneration);
			}else{
				toast("取消发布！");
			}
		}); 
		
	})
	
	
},false);

//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
//	console.log(positions+'--'+counts);
	if(files.length <= 0){
		toast("请上传头像！");
	}
//				console.log(JSON.stringify(files.path));return;
	var server = apiRoot + "?act=sns_album&op=file_uploadone&member_id="+member_id;  
	var task = plus.uploader.createUpload(server,
	    {method:"post"},
		function(t,status){ //上传完成
			if(status == 200){ 
				plus.nativeUI.closeWaiting();
//					console.log(obj);
				var infopics = $.parseJSON(t.responseText);
					console.log(JSON.stringify(infopics));
				if(infopics.code == 200){
					pictures += infopics.datas.file_name + ',';
					$('.'+obj).prepend('<img src="'+ infopics.datas.file_url +'" data-preview-src="" data-preview-group="1"/>');
				}else{
					toast(infopics.datas.error);
				}
//					plus.nativeUI.toast('处理完成' + t.responseText);
			}else{
				toast('上传失败：' + status);
				plus.nativeUI.closeWaiting();
			}
			
		},function () {
			console.log(123);
			plus.nativeUI.closeWaiting();
		}
		
	); 
	for(var i = 0;i　<　files.length ; i++){
		var f =files[i];
		task.addFile(f.path,{key:'file'});
//					    console.log(f.name)
	}
	task.start();
	 
}